<?php

namespace app\admin\controller;

use app\common\model\Base;
use app\common\thirdparty\Date;
use think\console\command\make\Model;
use think\Controller;
use think\captcha\Captcha;
use app\admin\model\Users;
use think\Exception;

class Index extends Controller
{
    public function index()
    {
        if (session("my_self"))//如果是登录状态直接跳转到home页
        {
            header("location:/admin/index/home/");
            exit;
        }
        return $this->fetch();
    }


    /**
     *  验证码
     */
    public function verify()
    {
        $captcha           = new Captcha();
        $captcha->fontSize = 28;
        $captcha->length   = 4;
        $captcha->useCurve = false;
        $captcha->useNoise = false;
        return $captcha->entry();
    }


    /**
     *  检查登录
     */
    public function checkLogin()
    {
        $m = new Users();
        return $m->checkLogin();
    }


    /**
     *
     * @return type
     */
    public function home()
    {
        if (!session("my_self"))//退出之后跳转登录页
        {
            header("location:/admin/index/");
            exit;
        }
        return $this->fetch();
    }


    /**
     *
     * @return 默认页
     */
    public function console()
    {
        $m_it    = new \app\common\model\IconTypes();
        $m_m     = new \app\common\model\Marks();
        $m_users = new Users();

        $marks_total = $m_m->_countWhere();
        $it_total    = $m_it->_countWhere();
        $users_total = $m_users->where('id','gt','1')->count();

        $line_total  = $m_m->_countWhere("areashape=3");
        $area_total  = $m_m->_countWhere("areashape<2 and length(linepoints)>0 or areashape=2");
        $point_total = $m_m->_countWhere("areashape=0 and length(linepoints)=0");


        return $this->fetch('', compact('marks_total', 'it_total', 'users_total', 'line_total', 'area_total', 'point_total'));
    }


    /**
     * 退出
     */
    public function logout()
    {
        cookie("user", null);
        session("my_self", null);
        session("my_menus", null);
        $this->success('退出成功', url("/admin/"));
    }


    public function clean()
    {

        $models = [new \app\common\model\Clients(), new \app\common\model\Marks(), new \app\common\model\MachineWithClient(), new \app\common\model\Maintains(), new \app\common\model\LogMaintains(), new \app\common\model\LogClients(), new \app\common\model\Companys(), new \app\admin\model\Files(),];
        $m      = new Base();
        $m->startTrans();
        try {
            array_map(function ($val) {
                $val->where('id', '>', '0')->delete();
            }, $models);
            $m->commit();
        } catch (Exception $e) {
            $m->rollback();
            return 'failed';
        }
        return 'success';
    }

}
